﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_RebateReport")]
    public class T_RebateReport : IEntity<T_RebateReport>
    {
        [Key]
        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(10)]
        public string Year { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(10)]
        public string Month { get; set; }

        public int DormitoryId { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        public int DealOrders { get; set; }

        public int SuccessOrders { get; set; }

        public int CancelOrders { get; set; }

        public decimal SuccessMoney { get; set; }

        public decimal Rebate { get; set; }

        public decimal Bonus { get; set; }

        [Column(TypeName = "datetime")]
        public DateTime CreateDate { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string CreateUser { get; set; }

        public override T_RebateReport Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_RebateReport Find(System.Linq.Expressions.Expression<Func<T_RebateReport, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.RebateReport.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.Year = item.Year;
                    this.Month = item.Month;
                    this.DormitoryId = item.DormitoryId;
                    this.UserName = item.UserName;
                    this.DealOrders = item.DealOrders;
                    this.SuccessOrders = item.SuccessOrders;
                    this.CancelOrders = item.CancelOrders;
                    this.SuccessMoney = item.SuccessMoney;
                    this.Rebate = item.Rebate;
                    this.Bonus = item.Bonus;
                    this.CreateDate = item.CreateDate;
                    this.CreateUser = item.CreateUser;

                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_RebateReport, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.RebateReport.Any(where);
            }
        }

        public override T_RebateReport FindLast(System.Linq.Expressions.Expression<Func<T_RebateReport, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.RebateReport.OrderByDescending(c => c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.Year = item.Year;
                    this.Month = item.Month;
                    this.DormitoryId = item.DormitoryId;
                    this.UserName = item.UserName;
                    this.DealOrders = item.DealOrders;
                    this.SuccessOrders = item.SuccessOrders;
                    this.CancelOrders = item.CancelOrders;
                    this.SuccessMoney = item.SuccessMoney;
                    this.Rebate = item.Rebate;
                    this.Bonus = item.Bonus;
                    this.CreateDate = item.CreateDate;
                    this.CreateUser = item.CreateUser;
                    return this;
                }
                return null;
            }
        }
    }
}
